package com.znyj.servlet.mainController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.entity.ReportEntity;
import com.znyj.servlet.service.ReportService;
import com.znyj.servlet.thrift.ThriftClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gaoliwei02 on 17/6/28.
 */
@Controller
@RequestMapping(value = "/single")
public class SingleController {
    private static final Logger logger = LoggerFactory.getLogger(SingleController.class);

    @Resource
    private ReportService reportService;

    @RequestMapping(value = "/{device_id}", method = RequestMethod.GET)
    protected String singlePage(@PathVariable(value = "device_id") String deviceId, HttpServletRequest request) throws ParseException, IOException {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("deviceId", deviceId);
        request.setAttribute("model", model);
        return "/page/single_page";
    }

    /*
    @RequestMapping(value = "/ctrl.inner", method = RequestMethod.POST)
    protected @ResponseBody String ctrlInner(@RequestBody String receiveData, HttpSession session) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject putJson = new JSONObject();

        String click = getJson.getString("click");
        String deviceId = getJson.getString("deviceId");
        int which = (getJson.getInteger("which"));
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        String userId = (String) session.getAttribute("username");
        if(userId == null){
            userId = "user page";
        }
        switch (which) {
            case 0: {
                if (click.equals("open")) {
                    logger.info("thrift page open all");
                    putJson = ThriftClient.taskSendThriftApi(userId, deviceId, timestamp.getTime(), 0, "cdbopen", "");
                }else if (click.equals("close")) {
                    logger.info("thrift page close all");
                    putJson = ThriftClient.taskSendThriftApi(userId, deviceId, timestamp.getTime(), 0, "cdbclose", "");
                }
                break;
            }

            case 1: {
                if (click.equals("open")) {
                    logger.info("thrift page open a");
                    putJson = ThriftClient.taskSendThriftApi(userId, deviceId, timestamp.getTime(), 0, "cdbaopen", "");
                }else if (click.equals("close")) {
                    logger.info("thrift page close a");
                    putJson = ThriftClient.taskSendThriftApi(userId, deviceId, timestamp.getTime(), 0, "cdbaclose", "");
                }
                break;
            }

            case 2: {
                if (click.equals("open")) {
                    logger.info("thrift page open b");
                    putJson = ThriftClient.taskSendThriftApi(userId, deviceId, timestamp.getTime(), 0, "cdbbopen", "");
                }else if (click.equals("close")) {
                    logger.info("thrift page close b");
                    putJson = ThriftClient.taskSendThriftApi(userId, deviceId, timestamp.getTime(), 0, "cdbbclose", "");
                }
                break;
            }
        }
        return putJson.toString();
    }
*/

    @RequestMapping(value = "/getinfo.inner", method = RequestMethod.POST)
    protected @ResponseBody String getinfoInner(@RequestBody String receiveData) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        String deviceId = getJson.getString("deviceId");

        ReportEntity reportEntity = reportService.findByDeviceId(deviceId);
        Gson gson = new GsonBuilder().create();

        return gson.toJson(reportEntity);
    }

    @RequestMapping(value = "/change_devid.inner", method = RequestMethod.POST)
    protected @ResponseBody String changeDevidInner(@RequestBody String receiveData, HttpSession session) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject putJson = new JSONObject();

        String deviceId = getJson.getString("deviceId");
        Long newid = Long.parseLong(getJson.getString("newid"));

        //Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        putJson = ThriftClient.taskSendThriftApi("fix", deviceId, newid, 0, "upcfg", "");
        return putJson.toString();
    }

    @RequestMapping(value = "/msg.inner", method = RequestMethod.POST)
    protected @ResponseBody String msgInner(@RequestBody String receiveData, HttpSession session) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject putJson = new JSONObject();

        String msg = getJson.getString("msg");
        String deviceId = getJson.getString("deviceId");

        if (msg.equals("")) {
            return null;
        } else {
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            putJson = ThriftClient.taskSendThriftApi((String) session.getAttribute("username"), deviceId, timestamp.getTime(), 0, msg, "");
        }
        return putJson.toString();
    }
}

